package main

//#link:https://leetcode-cn.com/problems/merge-two-sorted-lists/
type ListNode struct {
	Val  int
	Next *ListNode
}

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
	result := new(ListNode)
	temp := result
	for l1 != nil && l2 != nil {
		temp.Next = new(ListNode)
		temp = temp.Next
		if l1.Val < l2.Val {
			temp.Val = l1.Val
			l1 = l1.Next
		} else {
			temp.Val = l2.Val
			l2 = l2.Next
		}
	}

	for l1 != nil {
		temp.Next = new(ListNode)
		temp = temp.Next
		temp.Val = l1.Val
		l1 = l1.Next
	}
	for l2 != nil {
		temp.Next = new(ListNode)
		temp = temp.Next
		temp.Val = l2.Val
		l2 = l2.Next
	}
	return result.Next
}
